//
//  PYNavigationController.m
//  IHome
//
//  Created by zhujizheng on 2020/2/15.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "PYNavigationController.h"

@interface PYNavigationController () <UIGestureRecognizerDelegate>
@property (nonatomic, assign) BOOL isPushing;
/// 顶部细线
@property (nonatomic, strong) UIView *bottomLineView;
@end

@implementation PYNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationBar.tintColor = PY_Dark_Color;
    self.navigationBar.translucent = NO;
    self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:PY_Dark_Color, NSFontAttributeName:PY_Font_Bold(18)};
    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationBar setShadowImage:[UIImage new]];
    self.delegate = self;
    self.interactivePopGestureRecognizer.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initBottomLineView];
}

#pragma mark -- 是否允许手势启动
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // 如果是 NaivationController 的 根试图控制器, 则不允许启动手势
    if (self.viewControllers.count <= 1) {
        return NO;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:PYEdgeGestureBeginNotification object:nil];
    return YES;
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    
    if ([self isAnimating]) {
        // 有动画
        NSLog(@"正在动画，取消本次pop");
        return nil;
    }
    
    if ([self.viewControllers count] <= 1) {
        // 如果当前堆栈中只有一个，则不用pop
        NSLog(@"如果当前堆栈中只有一个，则不用pop");
        return nil;
    }
    return [super popViewControllerAnimated:animated];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if ([self isAnimating]) {
        // 有动画
        NSLog(@"正在动画，取消本次push");
        return;
    }
    self.isPushing = animated ? animated : _isPushing;
    [super pushViewController:viewController animated:animated];
}

- (BOOL)isAnimating
{
    return self.isPushing || [self isTransitioning];
}

- (BOOL)isTransitioning
{
    NSNumber *number = [self valueForKey:@"_isTransitioning"];
    return [number boolValue];
}

- (id)valueForUndefinedKey:(NSString *)key
{
    if ([key isEqualToString:@"_isTransitioning"]) {
        NSLog(@"KVC 找不到key:%@", key);
        return @"NO";
    }
    return [super valueForUndefinedKey:key];
}

#pragma mark - bottomLineView
- (UIView *)bottomLineView
{
    if (!_bottomLineView) {
        _bottomLineView = [UIView new];
        _bottomLineView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
        _bottomLineView.hidden = YES;
        _bottomLineView.alpha = 0;
    }
    return _bottomLineView;
}

- (void)initBottomLineView
{
    if (self.bottomLineView) {
        return;
    }
    [self.navigationBar addSubview:self.bottomLineView];
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
}
/// 设置侧滑手势是否可用
- (void)setBackEdgeGestureEnabled:(BOOL)enabled
{
    self.interactivePopGestureRecognizer.enabled = enabled;
}
#pragma mark - 旋转
// iPad屏幕尺寸发生变化，分屏，横屏
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    PYWeakSelf
    [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        [weakSelf.view setNeedsLayout];
    }];
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    self.isPushing = NO;
    if ([viewController isKindOfClass:[PYViewController class]]) {
        
    }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController isKindOfClass:[PYViewController class]]) {
        
    }
}

#pragma mark - 屏幕旋转
- (BOOL)shouldAutorotate
{
    return [PYScreenRotateManager shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
//    if ([PYScreenRotateManager supportedInterfaceOrientations] == UIInterfaceOrientationMaskPortrait) {
//        [UIApplication sharedApplication].statusBarOrientation = [PYScreenRotateManager statusBarOrientation];
//    }
    return [PYScreenRotateManager supportedInterfaceOrientations];
}
/// 实现这个方法，解决导航栏问题,present 
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
//{
//    return [PYScreenRotateManager statusBarOrientation];
//}

#pragma mark - MLeaksFinder methods
- (BOOL)willDealloc {
    return NO;
}
@end
